package org.itboys.mobile.service.mongo.mobile;


import org.itboys.commons.ErrorException;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.TianJieWalletProductMapper;
import org.itboys.mobile.dto.TianJieFinanceGameDto;
import org.itboys.mobile.entity.mongo.TianJieFinanceGame;
import org.itboys.mobile.entity.mysql.TianJieWallet;
import org.itboys.mobile.entity.mysql.TianJieWalletProduct;
import org.itboys.mobile.service.mysql.mobile.TianJieMobileWalletService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：天街_游戏_service
 */
@Service
public class TianJieMobileGameService extends BaseMongoService<TianJieFinanceGame> {
    @Autowired
    private TianJieMobileWalletService walletService;
    @Autowired
    private TianJieWalletProductMapper walletProductMapper;

    @Autowired
    private ResourceHolder resourceHolder;

    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }

    @Override
    protected Class<TianJieFinanceGame> getEntityClass() {
        return TianJieFinanceGame.class;
    }


    /**
     * 获取游戏列表
     * @param memberId
     * @return
     */
    public List<TianJieFinanceGameDto> getGames(Long memberId){
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        return packageDtoMuilt(super.list(param));
    }
    /**
     * 游戏充值
     * @param memberId
     * @param chargeProductId
     * @param account
     * @return
     */
    public void  charge(Long memberId,Long chargeProductId,Long gameId,String account){
       TianJieWalletProduct product=walletProductMapper.find(chargeProductId);
       if(product.getGameAmount()==null){
           throw new ErrorException("充值产品有误，请重试");
       }
        TianJieWallet wallet=walletService.findByMemberId(memberId);
        TianJieFinanceGame game= super.getById(gameId);

        if(product.getAmount().compareTo(wallet.getUnreleaseAmount())==1){
            throw new ErrorException("可用（待释放）余额不足");
        }
        walletService.generateRecord(wallet,memberId,product.getAmount(),SundayCommonConstants.wallet_record_type_game,"游戏充值-"+game.getName()+"-"+account,0l);
    }
    /**
     * 组装游戏
     * @return
     */
    public List<TianJieFinanceGameDto> packageDtoMuilt(List<TianJieFinanceGame> games){
        String imageRoot =resourceHolder.getStringValue("imgRoot");
        List<TianJieFinanceGameDto> dtos = new ArrayList<>();
        for(TianJieFinanceGame game:games){
            dtos.add(new TianJieFinanceGameDto(game.getId(), game.getCreateTime(),game.getCreateDate(),
                    game.getName(),imageRoot+game.getImage(),game.getIosUrl(), game.getAndroidUrl(),game.getSchema(),game.getDesc()));
        }
        return dtos;
    }





}
